<?php 
defined('IN_DESTOON') or exit('Access Denied');
class childcompany {
	var $itemid;
	var $db;
	var $table;
    var $order_table;
	var $fields;
	var $errmsg = errmsg;

    function childcompany() {
		global $db;
		$this->table = $db->pre.'member';
        $this->table_company = $db->pre.'company';
        $this->order_table = $db->pre.'buy_6';
		$this->db = &$db;
		$this->fields = array('listorder', 'userid', 'username','passport' ,'password', 'truename', 'gender', 'areaid', 'department', 'career','mobile','email','qq','company', 'parent_id','passsalt','paysalt','payword');
                $this->fields_company = array('userid', 'username', 'company', 'groupid', 'type', 'size', 'telephone');
    }

	function pass($post) {
		global $_userid, $L;
		if(!is_array($post)) return false;
		if(!$post['truename']) return $this->_($L['friend_pass_truename']);
		return true;
	}

	function set($post) {
		if($post['email'] && !is_email($post['email'])) $post['email'] = '';
		if($post['msn'] && !is_email($post['msn'])) $post['msn'] = '';
		if($post['qq'] && !is_numeric($post['qq'])) $post['qq'] = '';
		$post = dhtmlspecialchars($post);
		return array_map("trim", $post);
	}

	function get_one($condition = '') {
            return $this->db->get_one("SELECT * FROM {$this->table} WHERE userid=$this->itemid $condition");
	}

    function get_oneorder($condition = '') {
        return $this->db->get_one("SELECT * FROM {$this->order_table} WHERE itemid=$this->itemid $condition");
    }

	function get_list($condition = 'status=3', $order = 'itemid DESC') {
		global $TYPE, $pages, $page, $pagesize, $offset, $L, $sum;
		if($page > 1 && $sum) {
			$items = $sum;
		} else {
			$r = $this->db->get_one("SELECT COUNT(*) AS num FROM {$this->table} WHERE $condition");
			$items = $r['num'];
		}
		$pages = pages($items, $page, $pagesize);
		if($items < 1) return array();
		$lists = array();
		$result = $this->db->query("SELECT * FROM {$this->table} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while($r = $this->db->fetch_array($result)) {
			$r['adddate'] = timetodate($r['regtime'], 5);
			//$r['type'] = $r['typeid'] && isset($TYPE[$r['typeid']]) ? set_style($TYPE[$r['typeid']]['typename'], $TYPE[$r['typeid']]['style']) : $L['default_type'];
			$lists[] = $r;
		}
		return $lists;
	}

	function add($post) {
                global $DT, $DT_TIME, $DT_IP, $MOD, $L;
		$post = $this->set($post);
                 
		$sqlk = $sqlv = '';
		foreach($post as $k=>$v) {
			if(in_array($k, $this->fields)) { $sqlk .= ','.$k; $sqlv .= ",'$v'"; }
		}
              
                $sqlk = substr($sqlk, 1);
                $sqlv = substr($sqlv, 1);
		$this->db->query("INSERT INTO {$this->table} ($sqlk,groupid,regip,regtime,loginip,logintime)  VALUES ($sqlv,8,'$DT_IP','$DT_TIME','$DT_IP','$DT_TIME')");
             
                $user_id = $this->db->insert_id();
                if ($user_id > 0) {
                    // 公司表 
                    $this->db->query("INSERT INTO {$this->table_company} (userid,username,company) VALUES ($user_id,'{$post['username']}','{$post['company']}')");
                    
                }
	       return $user_id;
	}

	function edit($post) {
	    $post = $this->set($post);
            $sql = '';
            foreach ($post as $k => $v) {
                if (in_array($k, $this->fields))
                    $sql .= ",$k='$v'";
            }
            $sql = substr($sql, 1);
            $this->db->query("UPDATE {$this->table} SET $sql WHERE userid=$this->itemid");
            return true;
    }

	function delete($itemid) {
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$this->db->query("DELETE FROM {$this->table} WHERE itemid IN ($itemids)");
	}

    function  update_order($itemid){
        $itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
        $r = $db->get_one("update {$this->order_table} set status=7 WHERE itemid IN ($itemids) ");
    }

	function _($e) {
		$this->errmsg = $e;
		return false;
	}

    function get_orderlist($condition, $order = 'itemid DESC') {
        global $MOD, $pages, $page, $pagesize, $offset, $sum;
        if($page > 1 && $sum) {
            $items = $sum;
        } else {
            $r = $this->db->get_one("SELECT COUNT(*) AS num FROM {$this->order_table} WHERE $condition");
            $items = $r['num'];
        }
        $pages = pages($items, $page, $pagesize);
        if($items < 1) return array();
        $lists = array();
        $result = $this->db->query("SELECT * FROM {$this->order_table}  WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
        while($r = $this->db->fetch_array($result)) {
            $r['adddate'] = timetodate($r['addtime'], 5);
            $r['editdate'] = timetodate($r['edittime'], 5);
            $lists[] = $r;
        }
        return $lists;
    }
}
?>